package myself.draw.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import myself.draw.dao.DrawDao;
import myself.entity.ExamQuestionEntity;

@Repository
public class DrawDaoImpl implements DrawDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	public List<ExamQuestionEntity> qryAllQuestins(String teacherCode,
			int page, int everyPageCount,String  topic) {

		String sql = "select id,title,options,answer,title_type from ksxt_examination_question where title_isDelete = '0' and author_code = ? ";
        
		if (!topic.equals("0")) {
			sql = sql + " and title_type = ? limit ?,?";
			return this.jdbcTemplate.query(sql, new Object[] { teacherCode ,topic, page,
					everyPageCount}, new QList());
		}
		
		sql = sql +" limit ?,?"; 
		
		return this.jdbcTemplate.query(sql, new Object[] { teacherCode, page,
				everyPageCount }, new QList());
	}

	/**
	 * 使用了内部类的方法 使这个可以实现ROWMapper接口。实现了多重继承。
	 * 
	 * */
	private class QList implements RowMapper<ExamQuestionEntity> {

		public ExamQuestionEntity mapRow(ResultSet rs, int rowNum)
				throws SQLException {

			ExamQuestionEntity examQuestionEntity = new ExamQuestionEntity();
            
			examQuestionEntity.setId(rs.getInt("id"));
			examQuestionEntity.setTitle(rs.getString("title"));
			examQuestionEntity.setOptions(rs.getString("options"));
			examQuestionEntity.setAnswer(rs.getString("answer"));
			examQuestionEntity.setTitleType(rs.getString("title_type"));

			return examQuestionEntity;
		}

	}

	public int qryCount(String teacherCode, String topic) {

		String sql = "select count(1) from ksxt_examination_question where title_isDelete = '0' and author_code = ?";

		if (!topic.equals("0")) {
			sql = sql + " and title_type = ?";
			return this.jdbcTemplate.queryForInt(sql, teacherCode, topic);
		}

		return this.jdbcTemplate.queryForInt(sql, teacherCode);
	}

	
	public int delById(Integer id) {
		String  sql = "update ksxt_examination_question set title_isDelete = '1' where id = ?";
		
		
		return this.jdbcTemplate.update(sql, new Object[]{id});
	}
	
	
}
